package cn.shopping.controller.Franchisees;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.shopping.pojo.Sh_franchisees;
import cn.shopping.pojo.Sh_results;
import cn.shopping.pojo.Sh_total;
import cn.shopping.service.Franchisees.FranchiseesService;

@Controller
@RequestMapping("/ranchisees")
public class FranchiseesController {

	@Resource
	private FranchiseesService franchiseesService;
	/**
	 * 加盟店列表
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping("/getallfranchis.html")
	public String selallfranchisees(HttpSession session, Model model) {
		List<Sh_franchisees> list = franchiseesService.selallfranchisees();
		for (Sh_franchisees sh_franchisees : list) {
			System.out.println(sh_franchisees.getFranchisees_name());
		}
		model.addAttribute("franlist", list);
		return "jiamengdianliebiao";
	}
	
	/**
	 * 加盟店添加
	 */
	
	
	
	@RequestMapping("/addfranchis.html")
	public String addfranchis(@RequestParam("franceName") String franchName,
			@RequestParam("francePhone") String phone,
			@RequestParam("s_province") String province,
			@RequestParam("s_city") String city,
			@RequestParam("s_county") String areas,
			@RequestParam("frinsemail") String email,
			@RequestParam("franslist") String xiangqing,
			HttpSession session, Model model) {
		Sh_total totle = new Sh_total();
		totle.setSh_province(province);
		totle.setSh_city(city);
		totle.setSh_area(areas);
		Sh_franchisees franchis = new Sh_franchisees();
		if (franchiseesService.addtotal(totle)) {
			
			franchis.setFranchisees_address(franchiseesService.selmaxtotalid());
			franchis.setFranchisees_name(franchName);
			franchis.setFranchisees_email(email);
			franchis.setFranchisees_phone(phone);
			franchis.setFranchisees_company_details(xiangqing);
			franchis.setStartday(new Date(System.currentTimeMillis()));
			if (franchiseesService.addfrance(franchis)) {
				return "redirect:/ranchisees/getallfranchis.html";
			}
			if (franchiseesService.deltotaladd(franchiseesService.selmaxtotalid())) {
				return "tianjiajiamengdian";
			}
		}
		return "tianjiajiamengdian";
	}
	
	
	@RequestMapping("/delfByid/{uid}")
	public String delfByid(@PathVariable Integer uid){
		int ids = uid;
		if (franchiseesService.delfranch(ids)) {
			return "redirect:/ranchisees/getallfranchis.html";
		}
		return "redirect:/ranchisees/getallfranchis.html";
	}
	
	
	/**
	 * 修改加盟店
	 */
	//获取数据
	
	@RequestMapping("/selfByid.html/{uid1}")
	public String selfByid(@PathVariable Integer uid1,Model model,HttpSession session){
		int ids = uid1;
		Sh_franchisees franchiees = franchiseesService.getfranchiseesbyId(ids);
		model.addAttribute("fses", franchiees);
		session.setAttribute("shtotal", franchiees.getTotals());
		return "jiamengdianxiugai";
	}
	
	
//	@RequestMapping("/selfByidjson/{uid2}")
//	@ResponseBody
//	public Map<String,Sh_franchisees> selfByidjson(@PathVariable Integer uid2){
//		int ids = uid2;
//		Sh_franchisees franchiees = franchiseesService.getfranchiseesbyId(ids);
//		Map<String, Sh_franchisees> map = new HashMap<String, Sh_franchisees>();
//		map.put("franchiees", franchiees);
//		return map;
//	}
	@RequestMapping("/selfByidjson/{uid2}")
	@ResponseBody
	public List<Sh_franchisees> selfByidjson(@PathVariable Integer uid2){
		int ids = uid2;
		Sh_franchisees franchiees = franchiseesService.getfranchiseesbyId(ids);
		List<Sh_franchisees> list = new ArrayList<Sh_franchisees>();
		list.add(franchiees);
		return list;
	}
	
	//修改储存
	@RequestMapping("/modifyfranchs.html")
	public String modifyfranchs(@RequestParam("franchid") Integer franchid,
			@RequestParam("franName") String franchName,
			@RequestParam("franPhone") String phone,
			@RequestParam("shengfen") String province,
			@RequestParam("chengshi") String city,
			@RequestParam("quyu") String areas,
			@RequestParam("email") String email,
			@RequestParam("franList") String xiangqing,
			HttpSession session, Model model){
		Sh_franchisees franchiees = franchiseesService.getfranchiseesbyId(franchid);
		Sh_total totals = franchiees.getTotals();
		totals.setSh_province(province);
		totals.setSh_city(city);
		totals.setSh_area(areas);
		if (franchiseesService.modifytotal(totals)) {
			franchiees.setFranchisees_name(franchName);
			franchiees.setFranchisees_phone(phone);
			franchiees.setFranchisees_company_details(xiangqing);
			franchiees.setFranchisees_email(email);
			franchiees.setModifyday(new Date(System.currentTimeMillis()));
			if (franchiseesService.modifyfranch(franchiees)) {
				return "redirect:/ranchisees/getallfranchis.html";
			}
		}
		Sh_total totals2 = (Sh_total)session.getAttribute("shtotal");
		franchiseesService.modifytotal(totals2);
		return "redirect:/ranchisees/selfByid.html/"+franchid;
	}
	
	/**
	 * 业绩
	 */
	@RequestMapping("/getresults.html")
	public String getresults(Model model){
		List<Sh_franchisees> list = franchiseesService.selresults();
		model.addAttribute("results", list);
		return "jiamengdianyeji";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
